package com.hschinese.pronunciation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.bean.AppDataBean;
import com.hschinese.pronunciation.bean.AppResourceMap;
import com.hschinese.pronunciation.bean.TestResultBean;
import com.hschinese.pronunciation.utils.Constant;
import com.hschinese.pronunciation.utils.HsSoundUtil;
import com.hschinese.pronunciation.utils.Logger;

import java.util.List;


/**
 * Created by wo on 2015/11/3.
 */
public class PinyinResultAdapter extends BaseAdapter {
    private Context mContext = null;
    private List<TestResultBean> mData = null;
    private LayoutInflater inflater;

    public PinyinResultAdapter(List<TestResultBean> data, Context cxt) {
        mData = data;
        mContext = cxt;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;
        if (convertView == null) {
            mHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.pinyin_result_item, parent, false);
            mHolder.mPlayImg = (ImageView)convertView.findViewById(R.id.pinyin_item_img);
            mHolder.mPinyinText = (TextView)convertView.findViewById(R.id.pinyin_item_text);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder)convertView.getTag();
        }
        TestResultBean d = mData.get(position);
        String vals[] = d.getTone().replace("|", " ").split(" ");
        mHolder.mPinyinText.setText(vals[0]);
        final String audioPath = AppResourceMap.getMp3Fold(d.getType()) + vals[1] + ".mp3";
        mHolder.mPlayImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Logger.e("xionghy-pinyin-audioPath: " + audioPath);
                HsSoundUtil.playAsset(mContext, audioPath);
            }
        });
        return convertView;
    }

    class ViewHolder {
        public ImageView mPlayImg;
        public TextView mPinyinText;
    }
}
